package com.cjb.cloudsecurity.config;

import com.cjb.cloudsecurity.JWT.AuthenticationTokenFilter;
import com.cjb.cloudsecurity.exception.SecurityAuthenticationEntryPoint;
import com.cjb.common.util.JsonUtils;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.List;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class SecurityFilterConfig
 * @Date 2023-08-20 23:11
 */
@Configuration
@AllArgsConstructor
@EnableWebSecurity
@EnableMethodSecurity
@Slf4j
public class SecurityFilterConfig {

    /**
     * 注入token验证继承类
     */
    @Resource
    private AuthenticationTokenFilter authenticationTokenFilter;
    /**
     * 注入放行资源列表
     */
    @Resource
    private PermitResource permitResource;

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        // 忽略授权的地址列表
        List<String> permitList = permitResource.getPermitList();
        String[] permits = permitList.toArray(new String[0]);
        log.info("打印放行资源列表{}", JsonUtils.toJsonString(permits));
//        http
//                .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
//                .authorizeRequests()
//                .requestMatchers(permits).permitAll()
//                .anyRequest().authenticated()
//                .and()
//                .formLogin()
//                .permitAll()
//                .and()
//                .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
//                .csrf(AbstractHttpConfigurer::disable)
//                ;
        /**
         *  http
         *                 .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
         *                 .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
         *                 .authorizeHttpRequests(auth -> auth
         *                         .requestMatchers(permits).permitAll()
         *                         .requestMatchers(HttpMethod.OPTIONS).permitAll()
         *                         .anyRequest().authenticated()
         *                 )
         * */
        http
        .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers(permits).permitAll()
                        .requestMatchers(HttpMethod.OPTIONS).permitAll()
                        .anyRequest().authenticated()
                )
                .exceptionHandling(exception -> exception.authenticationEntryPoint(new SecurityAuthenticationEntryPoint()))
                .headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
                .csrf(AbstractHttpConfigurer::disable);
        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        log.info("确认密码加密这东西被加载了 / 验证方便暂时不加密了..");
        //return new BCryptPasswordEncoder();
        return NoOpPasswordEncoder.getInstance();
    }
}